/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$(function (){
    console.log('entro');
});
function AjaxCrearAccionMail(Formulario)
{
//    var listas = new Array();
    BloquearBotonesModal(Formulario);
    AjaxAccionModalMail(Formulario, function(data) {
//        ActualizarInformacionAccion(listas);
        console.log(data);
        var $button_action = $('#mail_cco_' + data.cco_id + '>button');
        if (!$button_action.hasClass('btn-primary')) {
            $button_action.addClass('btn-primary');
            $button_action.removeClass('btn-inverse');
        }
        $button_action.html('<i class="icon-envelope"></i> (' + data.count + ') ');
    });
}
function AjaxAccionModalMail($form, CallBack) {
    var form = $($form);
    var settings = form.data('settings');
    settings.submitting = true;
    $.fn.yiiactiveform.validate(form, function(messages) {
        $.each(messages, function() {
            console.log(this);
        });
        if ($.isEmptyObject(messages)) {
            $.each(settings.attributes, function() {

                $.fn.yiiactiveform.updateInput(this, messages, form);
            });
            AjaxGuardarAccionModalMail(true, $form, CallBack);

        }
        else {
            settings = form.data('settings'),
                    $.each(settings.attributes, function() {
                        $.fn.yiiactiveform.updateInput(this, messages, form);
                    });
            DesBloquearBotonesModal($form, 'Enviar', 'AjaxCrearAccionMail');
        }
    });
}
/**
 * @author Alex Yepez Chavez
 * @param {cadena} Formulario
 * Guarda el contenido de los modales de las acciones
 */
function AjaxGuardarAccionModalMail(verificador, Formulario, CallBack)
{
//    console.log($(Formulario).attr('action'));
    if (verificador)
    {
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: $(Formulario).attr('action'),
            data: $(Formulario).serialize(),
            beforeSend: function(xhr) {
            },
            success: function(data) {
                if (data.success) {
                    $("#mainModal").modal("hide");
                    $("#maiMessages").removeClass('hidden');
                    $("#maiMessages").html('<div class="alert alert-success">' +
                            '<i data-dismiss="alert" class="icon-remove close"></i>' +
                            data.messages.success.toString() +
                            '</div>');
                    CallBack(data);
                } else {
                    DesBloquearBotonesModal(Formulario);
                    bootbox.alert(data.messages.error.toString());
                    $("#mainModal").modal("hide");
                    CallBack(data);

                }
            }
        });
    }
}



